package transformer

import (
	"fmt"
	"kumachan/standalone/util/richtext"
	"kumachan/lang/source"
	"kumachan/lang/textual/cst"
	"kumachan/lang/textual/scanner"
)


type File struct {
	Key      source.FileKey
	Code     scanner.Code
	Info     scanner.RowColInfo
	SpanMap  scanner.RowSpanMap
}
func makeFile(tree *cst.Tree, key source.FileKey) *File {
	return &File {
		Key:     key,
		Code:    tree.Code,
		Info:    tree.Info,
		SpanMap: tree.SpanMap,
	}
}

func (f *File) GetKey() source.FileKey {
	return f.Key
}

func (f *File) DescribePosition(pos source.Position) string {
	var start = uint(pos.Index1)
	if start < uint(len(f.Info)) {
		var point = f.Info[start]
		return fmt.Sprintf("(row %d, column %d)", point.Row, point.Col)
	} else {
		return fmt.Sprintf("(token %d)", start)
	}
}

func (f *File) FormatMessage(pos source.Position, desc richtext.Block) richtext.Block {
	var span = scanner.Span {
		Start: int(pos.Index1),
		End:   int(pos.Index2),
	}
	var point scanner.Point
	if span.Start < len(f.Info) {
		point = f.Info[span.Start]
	}
	var name = f.Key.String()
	const FOV = 5
	return cst.FormatError (
		name, f.Code, f.Info, f.SpanMap,
		point, span, FOV, desc,
	)
}


